package com.myccb.app.widget;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myccb.app.R;

/**
 * Created by hyt on 2017/11/30.
 */

public class CommonToolBar extends LinearLayout {
    private Toolbar toolbar;
    private ImageView leftImg;
    private TextView title;
    private ImageView rightImg;


    private OnClickListener onLeftButtonClickListener;

    public CommonToolBar(Context context) {
        super(context);
    }


    public CommonToolBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.toolbar, this, true);

        toolbar=findViewById(R.id.toolbar);

        leftImg=findViewById(R.id.toolbar_left_img);
        leftImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftButtonClickListener!=null)
                    onLeftButtonClickListener.onClick(v);
            }
        });
        title=findViewById(R.id.toolbar_title);
        rightImg=findViewById(R.id.toolbar_right_img);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonToolBar);

        if (attributes != null) {
            int toolBarBackgroundColor=attributes.getColor(R.styleable.CommonToolBar_toolBarBackgroundColor,getResources().getColor(R.color.colorPrimary));

            toolbar.setBackgroundColor(toolBarBackgroundColor);

            int leftDrawableId=attributes.getResourceId(R.styleable.CommonToolBar_leftDrawable,0);
            if (leftDrawableId!=0){
                leftImg.setImageResource(leftDrawableId);
            }

            int titleColor=attributes.getColor(R.styleable.CommonToolBar_toolBarBackgroundColor, Color.WHITE);

            title.setTextColor(titleColor);

            String titleText=attributes.getString(R.styleable.CommonToolBar_titleText);
            if (TextUtils.isEmpty(titleText))
                title.setText(R.string.app_name);
            else
                title.setText(titleText);

            int rightDrawableId=attributes.getResourceId(R.styleable.CommonToolBar_leftDrawable,0);
            if (rightDrawableId!=0){
                rightImg.setImageResource(leftDrawableId);
            }
            attributes.recycle();
        }
        setElevation(0);
    }
    public Toolbar getToolbar(){
        return toolbar;
    }

    public void setTitle(String title){
        this.title.setText(title);
    }

    public void setOnLeftButtonClickListener(OnClickListener onLeftButtonClickListener){
        this.onLeftButtonClickListener=onLeftButtonClickListener;
    }
}